package Servlet.login_register;

import Dao.UserDao;
import Manage.User_login;
import Utils.mybatisUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class loginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User_login> list=new ArrayList<>();
        ServletContext application=request.getServletContext();
        String username="";
        String password="";
        String user="";
        user=request.getParameter("user");
        username=request.getParameter("username");
        password=request.getParameter("password");
        SqlSession session= mybatisUtils.getSqlSession();
        UserDao dao=session.getMapper(UserDao.class);
        list=dao.login(user,username,password);
        if (list.size()==0)
            request.getRequestDispatcher("/newerror_login.html").forward(request,response);
        else {
            application.setAttribute("user",user);
            application.setAttribute("username",username);
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
        session.close();
    }
}
